<?php
/*

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="ppform">
	<input type="hidden" id="cmd" name="cmd" value="_cart" />
	<input type="hidden" id="item_name" name="item_name" value="All Videos" />
	<input type="hidden" id="on0" name="on0" value="Type" />
   <input type="hidden" id="lc" name="lc" value="US" />
	<input type="hidden" id="no_note" name="no_note" value="1" />
	<input type="hidden" id="return" name="return" value="http://www.tents-of-mercy.org/resources/video-clips?type=1" />

	<input type="hidden" id="on0" name="on0" value="Type" />
   <select name="os0">
      <option value="NTSC">NTSC</option>
      <option value="PAL">PAL</option>
   </select>
	
	
	<input type="hidden" id="cancel_return" name="cancel_return" value="http://www.tents-of-mercy.org/resources/video-clips?type=2" />
	<input type="hidden" id="currency_code" name="currency_code" value="USD" />
	<input type="hidden" id="shipping" name="shipping" value="5" />
	<input type="hidden" id="add" name="add" value="1" />
	<input type="hidden" id="amount" name="amount" value="60" />
	<input type="hidden" id="business" name="business" value="chopfamily@yahoo.com" />
	<input type="hidden" id="shopping_url" name="shopping_url" value="http://www.tents-of-mercy.org/resources/video-clips" />
	<input type="image" id="ppform_submit" name="submit" border="0" src="https://www.paypal.com/en_US/i/btn/x-click-but22.gif" alt="PayPal - The safer, easier way to pay online" style="vertical-align: middle; padding: 0px; margin: 0px" />
</form>

*/

/*$pp = new App_Helper_Paypal('paynow', 'small');
$pp->addHidden(array(
   'item_name' => 'Test purchase',
   'business' => 'info@gorellnet.com',
   'amount' => '35',
));
$pp->addSelectOption('Type', 0, array('NTSC' => 'NTSC', 'PAL' => 'PAL'));

echo $pp;*/

class App_Helper_Paypal_CustomHtml {
   
   private $_data;
   
   public function __construct($data) {
      $this->_data = $data;
   }
   
   public function __toString() {
      return $this->_data;
   }
   
}


class App_Helper_Paypal_SelectOption {
   
   public $name;
   public $id;
   public $options;
   
   public function __construct($name, $id, $options=array()) {
      $this->name = $name;
      $this->id = $id;
      $this->options = $options;
   }
   
   public function addOption($name, $value) {
      $items = array();
      if(!is_array($name)) {
         $items[$name] = $value;
      } else {
         $items = $name;
      }
      foreach($items as $k => $v) {
         $this->options[$k] = $v;
      }
      
   }
   
   public function __toString() {
      $str = "\t<input type=\"hidden\" id=\"on$this->id\" name=\"on$this->id\" value=\"$this->name\" />\n" . 
         "\t<select name=\"os$this->id\">\n";
         
      foreach($this->options as $k => $v) {
         $str .= "\t\t<option value=\"$k\">$v</option>\n";
      }
      $str .= "\t</select>\n";
      return $str;
   }
   
}
   
   
class App_Helper_Paypal_Hidden {
   public $name;
   public $value;
   public function __construct($name, $value) {
      $this->name = $name;
      $this->value = $value;
   }
   public function __toString() {
      return "\t<input type=\"hidden\" name=\"$this->name\" value=\"$this->value\" />\n";
   }
}

class App_Helper_Paypal extends Zend_Controller_Action_Helper_Abstract {
   
   
   private $_data = array();
   private $_sandbox = false;
   private $_sandbox_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; 
   private $_sandbox_buyer = ''; //'buyer_1234646068_per@gorellnet.com';
   private $_sandbox_business = ''; //'seller_1234646093_biz@gorellnet.com';
   private $_url = 'https://www.paypal.com/cgi-bin/webscr'; 
   private $_image = ''; 
   private $_id;

   
   private $_images = array(
      'buynow' => array(
          'small' => 'btn_buynow_SM.gif',
          'large' => 'btn_buynow_LG.gif',
          'cc' 	=> 'btn_buynoCC_LG.gif',
          'square1' => 'x-click-but23.gif',
          'square2' => 'x-click-but9.gif',
          'square3' => 'x-click-butcc.gif',
          'square4' => 'x-click-but01.gif',
          'squere5' => 'x-click-but5.gif'
      ),
      'paynow' => array(
          'small' => 'btn_paynow_SM.gif',
          'large' => 'btn_paynow_LG.gif',
          'cc' 	=> 'btn_paynowCC_LG.gif',
          'square1' => 'x-click-but02.gif',
          'squere2' => 'x-click-but06.gif'
      ),
      'cart' => array(
          'small' => 'btn_cart_SM.gif',
          'large' => 'btn_cart_LG.gif',
          'cc' 	=> 'btn_cartCC_LG.gif',
          'square1' => 'x-click-but22.gif',
          'square2' => 'sc-but-01.gif'
      ),
      'viewcart' => array(
          'small' => 'btn_viewcart_SM.gif',
          'large' => 'btn_viewcart_LG.gif',
          'cc' 	=> 'btn_viewcartCC_LG.gif',
          'square1' => 'x-click-but03.gif'
      ),
   
      'donate' => array(
          'small' => 'btn_donate_SM.gif',
          'large' => 'btn_donate_LG.gif',
          'cc' 	=> 'btn_donateCC_LG.gif',
          'square1' => 'x-click-but04.gif',
          'square2' => 'x-click-but21.gif'
      ),
      'subscribe' => array(
          'small' => 'btn_subscribe_SM.gif',
          'large' => 'btn_subscribe_LG.gif',
          'cc' 	=> 'btn_subscribeCC_LG.gif',
          'square1' => 'x-click-but20.gif'
      ),
      'gift' => array(
          'small' => 'btn_gift_SM.gif',
          'large' => 'btn_gift_LG.gif',
          'cc' 	=> 'btn_giftCC_LG.gif'
      )
   
   );
   
   private $_params = array(
      'checkout' => array(
         'page_style' => '',
         'image_url' => '',
         'cpp_header_image' => '',
         'cpp_headerback_color' => '',
         'cpp_headerborder_color' => '',
         'cpp_payflow_color' => '',
         'cs' => '',
         'lc' => '',
         'no_note' => '',
         'cn' => '',
         'no_shipping' => '',
         'return' => '',
         'rm' => '',
         'cbt' => '',
         'cancel_return' => '',
         'address1' => '',
         'address2' => '',
         'city' => '',
         'country' => '',
         'first_name' => '',
         'last_name' => '',
         'charset' => '',
         'night_phone_a' => '',
         'night_phone_b' => '',
         'night_phone_c' => '',
         'state' => '',
         'zip' => ''
      ), 
      'transaction' => array(
         'address_override' => '',
         'currency_code' => '',
         'custom' => '',
         'handling' => '',
         'invoice' => '',
         'shipping' => '', // only for cart upload command
         'tax_cart' => '',
         'weight_cart' => '',
         'weight_unit' => '' // lbs, kg
      ),
      'item' => array(
         'item_name' => '',
         'item_number' => '',
         'quantity' => '',
         'shipping' => '',
         'shipping2' => '',
         'tax' => '',
         'tax_rate' => '',
         'undefined_quantity' => '',
         'weight' => '',
         'weight_unit' => '', // default=lbs, kg
         'on0' => '',
         'on1' => '',
         'on2' => '',
         'on3' => '',
         'on4' => '',
         'on5' => '',
         'on6' => '',
         'os0' => '',
         'os1' => '',
         'os2' => '',
         'os3' => '',
         'os4' => '',
         'os5' => '',
         'os6' => '',
         'option_index0' => '',
         'option_select0' => '',
         'option_select1' => '',
         'option_select2' => '',
         'option_select3' => '',
         'option_select4' => '',
         'option_select5' => '',
         'option_select6' => '',
         'option_select7' => '',
         'option_select8' => '',
         'option_select9' => '',
         'option_amount0' => '',
         'option_amount1' => '',
         'option_amount2' => '',
         'option_amount3' => '',
         'option_amount4' => '',
         'option_amount5' => '',
         'option_amount6' => '',
         'option_amount7' => '',
         'option_amount8' => '',
         'option_amount9' => ''
      ),
      'cart' => array (
         'add' => '1',
         'display' => '',
         'upload' => '',
         'amount' => '',
         'business' => '',
         'handling_cart' => '',
         'item_name' => '',
         'paymentaction' => '',// sales, authorization, order
         'shopping_url' => ''
      ),
      'subscribe' => array (
         'business' => '',
         'item_name' => '',
         'currency_code' => '',
         'a1' => '',
         'p1' => '',
         't1' => '', // D=1-90, W=1-52, M=1-24, Y=1-5
         'a2' => '',
         'p2' => '',
         't2' => '',// D=1-90, W=1-52, M=1-24, Y=1-5
         'a3' => '',
         'p3' => '',
         't3' => '',// D=1-90, W=1-52, M=1-24, Y=1-5
         'src' => '',
         'srt' => '', // sales
         'sra' => '',
         'no_note' => '', //1,
         'custom' => '',
         'invoice' => '',
         'modify' =>  0,// 0, 1, 2
         'usr_manage' => ''
      )
   );
   
   private $_currencies = array(
      'USD' => 'US Dollars',
      'EUR' => 'Euros',
      'GBP' => 'Pounds Sterling',
      'CAD' => 'Canadian Dollars',
      'JPY' => 'Japanese Yen',
      'HKD' => 'Hong Kong Dollars',
      'AUD' => 'Australian Dollars',
      'NZD' => 'New Zeland Dollars',
      'SGD' => 'Singapore Dollars',
      'CHF' => 'Swiss Francs',
      'SEK' => 'Sweedish Kroner',
      'NOK' => 'Norwegian Kroner',
      'DKK' => 'Danish Kroner',
      'CZK' => 'Czek Kroner',
      'PLN' => 'Polish Zlotych'
   );
   
   public function __construct($type = 'buynow', $imgtype='small', $sandbox=false, $formId='ppform') {
     $config = Zend_Registry::get('config');
     $this->_data = array(
         'hidden' => array(
            'currency_code' => 'USD',
            'lc' => 'US',
            'shopping_url' => "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}",
            'return' => "http://{$_SERVER['HTTP_HOST']}/courses/thankyou",
            'no_note' => 1,
            'notify_url' => "http://{$_SERVER['HTTP_HOST']}/ipn",
            'business' => $config['site']['email']['paypal']['business']
         )
      );
      
      switch($type) {
         case 'buynow':
         case 'paynow':
             $this->addHidden('cmd', '_xclick');
             $this->addHidden('add', 1);
             break;
         case 'donate':
             $this->addHidden('cmd', '_donations');
             break;
         case 'subscribe':
             $this->addHidden('cmd', '_xclick-subscriptions');
             $this->addHidden('no_note', 1);
             break;
         case 'gift':
             $this->addHidden('cmd', '_oe-gift-certificate');
             break;
         default:
         case 'viewcart':
         case 'cart':
             $this->addHidden('cmd', '_cart');
             break;
     }
     
     $this->_image = $this->_images[$type][$imgtype];
     $this->_sandbox = $sandbox;
     $this->_id = $formId;
     
     
     $this->_sandbox_business = $config['site']['email']['paypal']['sandbox'];
   }
   
   public function addSelectOption($name, $id, $options) {
      $this->addCustom(new App_Helper_Paypal_SelectOption($name, $id, $options));
   }
   
   public function addCustom($data) {
      $elem = $data;
      if(is_string($elem)) {
         $elem = new App_Helper_Paypal_CustomHtml($data);
      }
      $this->_data[count($this->_data)] = $elem;
   }
   
   
   public function addHidden($name, $value=null) {
      $item = array();
      if(!is_array($name)) {
         $item = array($name => $value);
      } else {
         $item = $name;
      }
      foreach($item as $k => $v) {
         $this->_data['hidden'][$k] = $v;
      }
   
   }
   
   public function createHidden($name, $value) {
      if(!is_array($name)) {
         $name = array($name => $value);
      }
      $str = "";
      foreach($name as $k => $v) {
         $str .= "\t<input type=\"hidden\" name=\"$k\" value=\"$v\" />\n";
      }
      return $str;
   }
   
   public function __toString() {
      $url = $this->_sandbox ? $this->_sandbox_url : $this->_url;
      $str = "\n<form action=\"{$url}\" method=\"post\"" . ($this->_id != '' ? " id=\"{$this->_id}\"" : '') . ">\n";
      
      // hidden elements 
      foreach($this->_data['hidden'] as $k => $v) {
         if($this->_sandbox && $k == 'business') {
            $v = $this->_sandbox_business;            
         }
         $str .= $this->createHidden($k, $v);
      }
      
      foreach($this->_data as $k => $v) {
         if($k == 'hidden') {
            continue;
         }
         if((gettype($v) == 'string' || gettype($v) == 'integer')) {            
            $str .= new App_Helper_Paypal_Hidden($k, $v);
         } else if(method_exists($v, '__toString')) {
            if($this->_sandbox && $v->name == 'business') {
               $v->value = $this->_sandbox_seller;
            }
            $str .= $v;
         } 
      }
      
      $str .= "\n\t<input type=\"image\" name=\"submit\" id=\"{$this->_id}_submit\" border=\"0\" src=\"https://www.paypal.com/en_US/i/btn/" . $this->_image . "\" alt=\"PayPal - The safer, easier way to pay online\" style=\"vertical-align: middle; padding: 0px; margin: 0px\" />\n";
      $str .= "</form>\n";
//      pr($this->_data);
      return $str;
   }
   
   
   
   
   
}